/*
 *  Copyright 2008 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package netease;

import java.util.List;

import org.apache.ibatis.ibator.api.IntrospectedColumn;
import org.apache.ibatis.ibator.api.dom.xml.Attribute;
import org.apache.ibatis.ibator.api.dom.xml.TextElement;
import org.apache.ibatis.ibator.api.dom.xml.XmlElement;
import org.apache.ibatis.ibator.generator.ibatis2.sqlmap.elements.AbstractXmlElementGenerator;

/**
 * 
 * @author Jeff Butler
 * 
 */
public class NeteaseSelectElementGenerator extends AbstractXmlElementGenerator {

	public NeteaseSelectElementGenerator() {
		super();
	}

	@Override
	public void addElements(XmlElement parentElement) {
		XmlElement answer = new XmlElement("select"); //$NON-NLS-1$

		answer.addAttribute(new Attribute(
				"id", introspectedTable.getSelectByExampleStatementId())); //$NON-NLS-1$
		if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
			answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
					introspectedTable.getResultMapWithBLOBsId()));
		} else {
			answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
					introspectedTable.getBaseResultMapId()));
		}

		/*
		 * String parameterType; if
		 * (introspectedTable.getRules().generatePrimaryKeyClass()) {
		 * parameterType = introspectedTable.getPrimaryKeyType(); } else { //
		 * select by primary key, but no primary key class. Fields // must be in
		 * the base record parameterType =
		 * introspectedTable.getBaseRecordType(); }
		 */

		answer.addAttribute(new Attribute("parameterClass", //$NON-NLS-1$
				"java.util.Map"));

		ibatorContext.getCommentGenerator().addComment(answer);

		StringBuilder sb = new StringBuilder();
		sb.append("select "); //$NON-NLS-1$
		List<IntrospectedColumn> allColum = introspectedTable.getAllColumns();
		final int length = allColum.size();
		for (int i = 0; i < length; i++) {
			IntrospectedColumn introspectedColumn = allColum.get(i);
			sb.append(introspectedColumn.getActualColumnName());
			if (i < length - 1) {
				sb.append(",");
			}
		}
		answer.addElement(new TextElement(sb.toString()));
		sb.setLength(0);
		sb.append("from "); //$NON-NLS-1$
		sb.append(introspectedTable
				.getAliasedFullyQualifiedTableNameAtRuntime());
		answer.addElement(new TextElement(sb.toString()));
		sb.setLength(0);
		sb.append("<![CDATA[ where server = #server# and day >= #startDay# and day <= #endDay#]]> "); //$NON-NLS-1$
		answer.addElement(new TextElement(sb.toString()));
		sb.setLength(0);
		XmlElement dynamic = new XmlElement("dynamic");
		for (IntrospectedColumn introspectedColumn : allColum) {
			String columnName = introspectedColumn.getActualColumnName();
			String typeName = introspectedColumn.getFullyQualifiedJavaType()
					.getShortName();
			if (typeName.equalsIgnoreCase("long")
					|| typeName.equalsIgnoreCase("date")
					|| columnName.equalsIgnoreCase("id")
					|| columnName.equalsIgnoreCase("server")) {
				continue;
			}
			String propName = introspectedColumn.getJavaProperty();
			XmlElement isnotempty = new XmlElement("isNotEmpty"); //$NON-NLS-1$
			isnotempty.addAttribute(new Attribute("prepend", " and "));
			isnotempty.addAttribute(new Attribute("property", propName));
			isnotempty.addElement(new TextElement("	<![CDATA[" + columnName
					+ " = #" + propName + "# ]]>"));
			dynamic.addElement(isnotempty);
		}
		answer.addElement(dynamic);
		if (ibatorContext.getPlugins()
				.sqlMapSelectByExampleWithoutBLOBsElementGenerated(answer,
						introspectedTable)) {
			parentElement.addElement(answer);
		}
	}
}
